from PySide2.QtWidgets import (QApplication, QMainWindow, QWidget, QStylePainter,
                                QHBoxLayout, QVBoxLayout, QGridLayout, QStyleOptionTab,
                                QTabWidget, QTabBar, QDateEdit, QLabel, QStyle,
                                QPushButton, QRadioButton, QButtonGroup)
from PySide2.QtCore import Qt, QDate
from PySide2.QtGui import QFont


class HorizontalTextTabBar(QTabBar):
    def paintEvent(self, event):
        painter = QStylePainter(self)
        option = QStyleOptionTab()
        for index in range(self.count()):
            self.initStyleOption(option, index)
            painter.save()
            option.text =''   # 清除文字，避免重复绘制
            painter.drawControl(QStyle.CE_TabBarTab, option)
            painter.setFont(QFont('Arial', 12))
            painter.drawText(self.tabRect(index), Qt.AlignCenter, self.tabText(index))
            painter.restore()


class mywindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('多界面')
        self.setGeometry(300, 80, 750, 550)
        # 中央部件(主界面)
        central = self.main_page()
        self.setCentralWidget(central)
    
    def main_page(self):
        '''主页面: 选项卡部件'''
        mainpage = QWidget()
        main_layout = QHBoxLayout(mainpage)
        # tabwidget:管理多个tab页面
        self.tabwidget = QTabWidget()
        self.tabwidget.setTabBar(HorizontalTextTabBar())  # 标签文字横向显示
        self.tabwidget.setTabPosition(QTabWidget.West)    # 标签在左侧
        self.tabwidget.setDocumentMode(True)        
        # 标签样式
        tab_origin_style = '''
            QTabBar::tab {
                margin: 5px;
                padding: 15px;
                border: 1px solid gray;
                border-radius: 5px;
                min-width: 100px;
            }
            QTabBar::tab:selected {
                background: lightblue;
            }
        '''
        self.tabwidget.setStyleSheet(tab_origin_style)
        self.tabwidget.currentChanged.connect(self.swap_page)
        # 各页面创建
        self.page_1 = self.create_page_1()
        self.page_2 = self.create_page_2()
        # 各页面添加到tabwidget
        self.tabwidget.addTab(self.page_1, 'tab_1')
        self.tabwidget.addTab(self.page_2, 'tab_2')
        # 加入布局
        main_layout.addWidget(self.tabwidget)
        return mainpage

    def swap_page(self, index):
        '''切换到指定页面'''
        self.tabwidget.setCurrentIndex(index)

    def create_page_1(self):
        '''创建页面:日期选择器,标签显示日期'''
        page = QWidget()
        vlayout = QVBoxLayout(page)
        # 标签
        self.date_label = QLabel()
        self.date_label.setFont(QFont("Arial", 12))
        self.date_label.setText(f"当前日期是： {QDate.currentDate().toString('yyyy-MM-dd')}")
        # 日期选择器
        self.dateedit = QDateEdit()
        self.dateedit.setCalendarPopup(True)
        self.dateedit.setDate(QDate.currentDate())
        self.dateedit.setFixedWidth(200)
        self.dateedit.setFont(QFont("Arial", 12))
        self.dateedit.dateChanged.connect(self.showdate)    
        # 加入布局
        vlayout.addSpacing(10)
        vlayout.addWidget(self.date_label)
        vlayout.addSpacing(30)
        vlayout.addWidget(self.dateedit)
        vlayout.addStretch()
        return page

    def showdate(self):
        '''显示日期'''
        date = self.dateedit.date().toString('yyyy-MM-dd')
        self.date_label.setText(f'当前日期是： {date}')

    def create_page_2(self):
        '''创建页面:日期选择器,标签显示日期'''
        page = QWidget()
        vlayout = QVBoxLayout(page)
        # 标签
        self.radio_label = QLabel()
        self.radio_label.setFont(QFont("Arial", 12))
        # 单选按钮组
        sports = ['跑步', '快走', '瑜伽', '游泳', '篮球','爬山', '网球', '乒乓球']
        gridlayout = QGridLayout()
        self.btngroup = QButtonGroup()
        self.btngroup.setExclusive(True)   # 确保互斥
        for i, sport in enumerate(sports):
            radio = QRadioButton(sport)
            radio.setFont(QFont('Arial', 12))
            self.btngroup.addButton(radio, id=i)
            gridlayout.addWidget(radio, i/3, i%3)
            # 默认第一个单选按钮选中
            if i == 0:
                radio.setChecked(True)
                self.radio_label.setText(f'当前选择的是： {sport}')
        self.btngroup.buttonClicked.connect(self.showchoice)        
        # 加入布局
        vlayout.addSpacing(10)
        vlayout.addWidget(self.radio_label)
        vlayout.addSpacing(30)
        vlayout.addLayout(gridlayout)
        vlayout.addStretch()
        return page

    def showchoice(self, option):
        '''单选按钮组选中时,标签显示选中内容'''
        self.radio_label.setText(f'当前选择的是： {option.text()}')


if __name__ == '__main__':
    app = QApplication()
    window = mywindow()
    window.show()
    app.exec_()